package com.aaa.bitmapcache;

import android.Manifest;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);

        initView();
        decodeBitmap();
        ImageCache.getInstance().init(this, getExternalFilesDir(null) + "/_bitmap");
    }

    /**
     * 初始化 recyclerView
     */
    private void initView() {
        RecyclerView rv = findViewById(R.id.rv);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        rv.setLayoutManager(linearLayoutManager);

        BitmapAdapter bitmapAdapter = new BitmapAdapter(this);
        rv.setAdapter(bitmapAdapter);
    }

    /**
     * 解析图片
     */
    private void decodeBitmap() {
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_mv);
        Log.e(TAG, "decodeBitmap: " + bitmap.getWidth() + " x " + bitmap.getHeight() + " x "
                + bitmap.getConfig() + ",内存总大小" + bitmap.getByteCount());
        Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.icon_mv_p);
        Log.e(TAG, "decodeBitmap1: " + bitmap1.getWidth() + " x " + bitmap1.getHeight() + " x "
                + bitmap1.getConfig() + ",内存总大小" + bitmap1.getByteCount());
        Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.icon_mv_w);
        Log.e(TAG, "decodeBitmap2: " + bitmap2.getWidth() + " x" + bitmap2.getHeight() + " x "
                + bitmap2.getConfig() + ",内存总大小" + bitmap2.getByteCount());
        /*
hdpi
inDensity=160
inTargetDensity=480
outWidth=600
2019-08-28 20:50:34.392 19170-19170/? E/MainActivity: decodeBitmap: 600 x 898 x ARGB_8888,内存总大小2155200
2019-08-28 20:50:34.408 19170-19170/? E/MainActivity: decodeBitmap1: 600 x 898 x ARGB_8888,内存总大小2155200
2019-08-28 20:50:34.421 19170-19170/? E/MainActivity: decodeBitmap2: 600 x898 x ARGB_8888,内存总大小2155200
xhdpi
inDensity=320
inTargetDensity=480
outWidth=450
2019-08-28 22:46:42.449 2179-2179/com.aaa.bitmapcache E/MainActivity: decodeBitmap: 450 x 674 x ARGB_8888,内存总大小1213200
2019-08-28 22:46:42.461 2179-2179/com.aaa.bitmapcache E/MainActivity: decodeBitmap1: 450 x 674 x ARGB_8888,内存总大小1213200
2019-08-28 22:46:42.469 2179-2179/com.aaa.bitmapcache E/MainActivity: decodeBitmap2: 450 x674 x ARGB_8888,内存总大小1213200
        * */
    }
}


